/*
 * @Author: szx
 * @Date: 2021-11-14 13:32:49
 * @LastEditTime: 2021-11-14 13:42:55
 * @Description:
 * @FilePath: \leetcode\500-599\572\572.js
 */

function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}

/**
 * @param {TreeNode} root
 * @param {TreeNode} subRoot
 * @return {boolean}
 */
var isSubtree = function (root, subRoot) {
    if (root == null) {
        return false;
    }
    return check(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
};

var check = function (root, subRoot) {
    if (!root && !subRoot) {
        return true;
    }
    if ((root && !subRoot) || (!root && subRoot) || root.val != subRoot.val) {
        return false;
    }
    return check(root.left, subRoot.left) && check(root.right, subRoot.right);
};
